package cj.web.tel.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.login.controllers.BaseController;
import cj.web.tel.domain.FloorInfor;
import cj.web.tel.service.FloorInforService;

@Path("/floorInfor")
public class FloorInforController extends BaseController{

	private static Logger logger = Logger.getLogger(FloorInforController.class.getName());
	
	@Autowired
	private FloorInforService floorInforService;
	
	@Get("show")
	public String show(Model model,FloorInfor floorInfor) {
		return "/views/tel/floorInforList.vm";
	}

	@Post("getList")
	public String queryList(FloorInfor floorInfor) {
	
		//当前页  
		floorInfor.setStart((floorInfor.getPage()-1)*floorInfor.getRows());
		floorInfor.setLength(floorInfor.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = floorInforService.query(floorInfor);
		List<FloorInfor> objs = (List<FloorInfor>) rep.get("floorInforlis");
		
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		return "/views/tel/floorInforDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,FloorInfor floorInfor,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		floorInfor.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		floorInfor.setCreateUser(returnSessionObj(request).getAccount());
		floorInfor.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			floorInforService.create(floorInfor);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		FloorInfor floorInfor = floorInforService.queryById(id);

		model.add("obj", floorInfor);
		return "/views/tel/floorInforEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,FloorInfor floorInfor,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		floorInfor.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			floorInforService.update(floorInfor);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			floorInforService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}
